
<template>
  <el-upload
    class="upload-demo"
    :action="uploadUrl"
    :on-remove="handleRemove"
    :on-success="handleUploadSuccess"
    :before-upload="beforeAvatarUpload"
    :data="uploadData"
    :headers="uploadHeader"
    :file-list="files"
    :multiple="true"
    list-type="picture"
  >
    <el-button size="small" type="primary">点击上传</el-button>
    <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过50m</div>
  </el-upload>
</template>

<script>
import { mapState } from "vuex";
export default {
  name: "MultiUpload",
  props: {
    fileList: Array
  },
  computed: {
    ...mapState({
      token: state => state.Authorization
    })
  },
  data() {
    return {
      uploadUrl: `${this.$basePath}?service=App.MaskMachines.UploadImage&api=193&token=this.token`,
      uploadData: {},
      uploadHeader: {
        token: this.token
      },
      dialogImageUrl: "",
      dialogVisible: false,
      files: [],
      resultimg: []
    };
  },
  watch: {
    fileList: {
      handler: function(val, oldVal) {
        console.log("fileList", val);
        const files = [];
        for (const file in val) {
          files.push({
            name: Math.random() + ".jpg",
            url: val[file]
          });
        }
        this.files = files;
      }
    }
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
        return false;
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
        return false;
      }
      const type = file.name.split(".");
      return true;
    },
    handleRemove(file, fileList) {
      this.fileList.splice(this.fileList.indexOf(file.url), 1);
      this.$emit("removeResult", this.fileList);
    },
    handleUploadSuccess(response) {
      this.$emit("childUploadSuccess", this.files, response.data.visit_path);
    }
  }
};
</script>

<style lang="less" scoped>
/deep/.el-upload-list__item {
  transition: none !important;
}
</style>
 

